package me.zhyd.oauth;

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.enums.scope.AuthWechatMpScope;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeChatOpenRequest;
import me.zhyd.oauth.utils.AuthScopeUtils;
import me.zhyd.oauth.utils.AuthStateUtils;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MainApp {
    public static void main(String[] args) {
        // 创建授权request
        AuthRequest authRequest = new AuthWeChatOpenRequest(AuthConfig.builder()
            .clientId("wx54fdc467c0004e20")
            .clientSecret("e90f02f0b609e1a96a9647b9621a3d67")
            .scopes(AuthScopeUtils.getScopes(AuthWechatMpScope.SNSAPI_BASE))
            .redirectUri("https://test.www.quickf.cn/rest/companytax/merge/chinatax/authorizecode?access_token=b3a4523b-29d4-4414-b52b-eecc2a43ff8c")
            .build());

        // 生成授权页面
        String state = AuthStateUtils.createState();
        authRequest.authorize(state);

        // 授权登录后会返回code（auth_code（仅限支付宝））、state，1.8.0版本后，可以用AuthCallback类作为回调接口的参数
        // 注：JustAuth默认保存state的时效为3分钟，3分钟内未使用则会自动清除过期的state
        AuthCallback authCallback = new AuthCallback();
        authCallback.setCode("tbc");
        authCallback.setState(state);
        AuthResponse response = authRequest.login(authCallback);
        System.out.println(response);
    }
}
